#!/bin/python
from vBaum import *
from math import *
from random import *
from MeshSplitter import split_minvolume


scaling = 0.4
offset = 5

print("generating ground")

noiseExt = int(log(300*scaling,2)+3)
noiseHeight = 45*scaling
noisePlane = FractalNoise(noiseExt, int(noiseHeight), 1.1, False)
noisePlane.setPosition(Vector3(0,0,0.5*noiseHeight))
noiseSize=pow(2,noiseExt-1)
noiseCut = Box(Vector3(noiseSize, noiseSize, noiseSize))
noiseCut.setPosition(Vector3(0,0,-30*scaling-noiseSize))

cutSphere = Sphere(noiseSize*0.7)

ground = noisePlane.subtract(noiseCut)
ground = ground.intersect(cutSphere)

print("generating house")

insideExt = Vector3(180,180,125)*scaling
insideMove = Vector3(0,0,insideExt.z)
outsideExt = Vector3(200, 200, 135)*scaling
outsideMove = Vector3(0,0,outsideExt.z)

doorExt = Vector3(50,60,100)*scaling
doorMove = Vector3(-100*scaling, -insideExt.y, doorExt.z)

windowExt = Vector3(40,40,40)*scaling
windowMove = Vector3(80*scaling, -insideExt.y, windowExt.z+100*scaling)
window2Move = Vector3(insideExt.x, 80*scaling, windowMove.z)

inside = Box(insideExt)
inside.setPosition(insideMove)

outside = Box(outsideExt)
outside.setPosition(outsideMove)

door = Box(doorExt)
door.setPosition(doorMove)

window = Box(windowExt)
window.setPosition(windowMove)

window2 = Box(windowExt)
window2.setPosition(window2Move)

outside = outside.subtract(door)
outside = outside.subtract(window)
outside = outside.subtract(window2)

outside.exportAsObj("outside", 10, 0)

print("splitting basement")

stones = split_minvolume(outside.surfels().thicken(offset), 1500)

stonesUnion = Volume()
for s in stones:
	stonesUnion = stonesUnion.union(s)

print("joining objects")

outside = outside.union(stonesUnion)

house = outside.subtract(inside)

world = house.union(ground)

world.exportAsObj("house", 10, 2)
